package com.jxdinfo.hussar.cloud.module.example.lift.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.cloud.module.example.lift.dao.LiftDao;
import com.jxdinfo.hussar.cloud.module.example.lift.dao.LiftTimeCountDao;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftTimeCountChartDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftTimeCountDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftTimeCountSearchDto;
import com.jxdinfo.hussar.cloud.module.example.lift.model.Lift;
import com.jxdinfo.hussar.cloud.module.example.lift.model.LiftHistoryCount;
import com.jxdinfo.hussar.cloud.module.example.lift.model.LiftTimeCount;
import com.jxdinfo.hussar.cloud.module.example.lift.service.LiftTimeCountService;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 升降机工时统计(LiftTimeCount)表服务实现类
 *
 * @author makejava
 * @since 2023-10-23 18:32:14
 */
@Service("liftTimeCountService")
public class LiftTimeCountServiceImpl extends ServiceImpl<LiftTimeCountDao, LiftTimeCount> implements LiftTimeCountService {
    @Resource
    private LiftTimeCountDao liftTimeCountDao;
    @Resource
    private LiftDao liftDao;
    @Override
    public List<LiftTimeCountChartDto> findByChart(LiftTimeCountSearchDto dto) {

        LambdaQueryWrapper<Lift> liftWrapper = new LambdaQueryWrapper<>();
        liftWrapper.eq(ObjectUtils.isNotEmpty(dto.getTyProjectCode()),Lift::getTyProjectCode,dto.getTyProjectCode())
                .eq(Lift::getTyDeleteStatus,0)
                .in(ObjectUtils.isNotEmpty(dto.getLiftId()),Lift::getId, dto.getLiftId().split(","));
        List<Lift> lifts = liftDao.selectList(liftWrapper);

        //封装查询条件
        LambdaQueryWrapper<LiftTimeCount> timeWrapper = new LambdaQueryWrapper<>();
        timeWrapper.eq(StrUtil.isNotBlank(dto.getTyProjectCode()),LiftTimeCount::getTyProjectCode,dto.getTyProjectCode())
                .between(LiftTimeCount::getWorkStartTime,dto.getWorkStartTime()+" 00:00:00",dto.getWorkStartTime()+" 23:59:59")
                .in(LiftTimeCount::getLiftId,lifts.stream().map(Lift::getId).collect(Collectors.toList()))
                .orderByDesc(LiftTimeCount::getWorkStartTime);
        List<LiftTimeCount> list = liftTimeCountDao.selectList(timeWrapper);
        List<LiftTimeCountDto> liftTimeCountList = BeanUtil.copyToList(list, LiftTimeCountDto.class);
        Map<String, List<LiftTimeCountDto>> map = liftTimeCountList.stream().collect(Collectors.groupingBy(data -> data.getLiftId()));

        List<LiftTimeCountChartDto> dataList = new ArrayList<>();

        for(Lift lift : lifts){
            LiftTimeCountChartDto chartDto = new LiftTimeCountChartDto();
            chartDto.setLiftId(lift.getId());
            chartDto.setLiftName(lift.getLiftName());
            List<LiftTimeCountDto> liftTimeCountDtoList = map.get(lift.getId());
            if(liftTimeCountDtoList == null) {
                liftTimeCountDtoList = new ArrayList<>();
            }
            chartDto.setChartDataList(liftTimeCountDtoList);
            dataList.add(chartDto);
        }
        return dataList;
    }
}

